/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;
import mx.edu.itschapala.sistemas.septimo.control.*;
import mx.edu.itschapala.sistemas.septimo.modelo.*;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.*;

/**
 *
 * @author Miguel Romero
 */
public class VentaCrearEditar extends javax.swing.JFrame {

    /**
     * Creates new form ClienteCrearEditar
     */
    
    private VentaLnLocal ventaControl;
    private ProductoLnLocal productoControl;
    private ClienteLnLocal clienteControl;
    private List<Producto> listaP;
    private List<Cliente> listaC;
    private TipoVentana tipoVentana;
    private Venta venta;
    
    public VentaCrearEditar(Venta venta, VentaLnLocal ventaControl, ProductoLnLocal productoControl, ClienteLnLocal clienteControl ,TipoVentana tipoVentana) {
        
        this.ventaControl = ventaControl;
        this.clienteControl = clienteControl;
        this.productoControl = productoControl;
        this.tipoVentana = tipoVentana;
        this.venta = venta;
        
        initComponents();
       
        jComboBox4.removeAll();
        listaC = clienteControl.cliengetTodos();
        for(Cliente c: listaC){
            jComboBox4.addItem(c.getNombre());
        }
        jComboBox8.removeAll();
        listaP = productoControl.getTodos();
        for(Producto p: listaP){
            jComboBox8.addItem(p.getNombre());
        }
        jLabel3.setText(new Date()+"");

        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                jLabel1.setText("REGISTRAR UNA VENTA");
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        Etiqueta1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        Etiqueta2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        Etiqueta3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        Etiqueta4 = new javax.swing.JLabel();
        jComboBox4 = new javax.swing.JComboBox();
        jPanel7 = new javax.swing.JPanel();
        Etiqueta5 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        Etiqueta6 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        Etiqueta7 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        Etiqueta8 = new javax.swing.JLabel();
        jComboBox8 = new javax.swing.JComboBox();
        jPanel12 = new javax.swing.JPanel();
        Etiqueta9 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel13 = new javax.swing.JPanel();
        Etiqueta10 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel14 = new javax.swing.JPanel();
        Etiqueta11 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel15 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(14, 1));

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Titulo VVentas");
        jPanel2.add(jLabel1);

        getContentPane().add(jPanel2);

        jPanel3.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta1.setText("ID de la Venta");
        jPanel3.add(Etiqueta1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jPanel3.add(jTextField1);

        getContentPane().add(jPanel3);

        jPanel4.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta2.setBackground(new java.awt.Color(204, 204, 0));
        Etiqueta2.setText("ID del Cliente");
        jPanel4.add(Etiqueta2);

        jTextField2.setBackground(new java.awt.Color(204, 204, 0));
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField2KeyPressed(evt);
            }
        });
        jPanel4.add(jTextField2);

        getContentPane().add(jPanel4);

        jPanel5.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta3.setBackground(new java.awt.Color(153, 153, 153));
        Etiqueta3.setText("ID del Producto");
        jPanel5.add(Etiqueta3);

        jTextField3.setBackground(new java.awt.Color(153, 153, 153));
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });
        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField3KeyPressed(evt);
            }
        });
        jPanel5.add(jTextField3);

        getContentPane().add(jPanel5);

        jPanel6.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta4.setBackground(new java.awt.Color(204, 204, 0));
        Etiqueta4.setText("Nombre del Cliente");
        jPanel6.add(Etiqueta4);

        jComboBox4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox4MouseClicked(evt);
            }
        });
        jComboBox4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox4ActionPerformed(evt);
            }
        });
        jPanel6.add(jComboBox4);

        getContentPane().add(jPanel6);

        jPanel7.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta5.setBackground(new java.awt.Color(204, 204, 0));
        Etiqueta5.setText("Sexo");
        jPanel7.add(Etiqueta5);

        jLabel5.setBackground(new java.awt.Color(204, 204, 0));
        jPanel7.add(jLabel5);

        getContentPane().add(jPanel7);

        jPanel9.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta6.setBackground(new java.awt.Color(204, 204, 0));
        Etiqueta6.setText("Telefono");
        jPanel9.add(Etiqueta6);

        jLabel6.setBackground(new java.awt.Color(204, 204, 0));
        jPanel9.add(jLabel6);

        getContentPane().add(jPanel9);

        jPanel10.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta7.setBackground(new java.awt.Color(204, 204, 0));
        Etiqueta7.setText("Dirección");
        jPanel10.add(Etiqueta7);

        jLabel7.setBackground(new java.awt.Color(204, 204, 0));
        jPanel10.add(jLabel7);

        getContentPane().add(jPanel10);

        jPanel11.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta8.setBackground(new java.awt.Color(153, 153, 153));
        Etiqueta8.setText("Nombre del Producto");
        jPanel11.add(Etiqueta8);

        jComboBox8.setBackground(new java.awt.Color(153, 153, 153));
        jPanel11.add(jComboBox8);

        getContentPane().add(jPanel11);

        jPanel12.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta9.setBackground(new java.awt.Color(153, 153, 153));
        Etiqueta9.setText("Precio");
        jPanel12.add(Etiqueta9);

        jLabel9.setBackground(new java.awt.Color(153, 153, 153));
        jPanel12.add(jLabel9);

        getContentPane().add(jPanel12);

        jPanel13.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta10.setBackground(new java.awt.Color(153, 153, 153));
        Etiqueta10.setText("Deptartamento");
        jPanel13.add(Etiqueta10);

        jLabel10.setBackground(new java.awt.Color(153, 153, 153));
        jPanel13.add(jLabel10);

        getContentPane().add(jPanel13);

        jPanel14.setLayout(new java.awt.GridLayout(1, 2));

        Etiqueta11.setBackground(new java.awt.Color(153, 153, 153));
        Etiqueta11.setText("Descripción");
        jPanel14.add(Etiqueta11);

        jLabel11.setBackground(new java.awt.Color(153, 153, 153));
        jPanel14.add(jLabel11);

        getContentPane().add(jPanel14);

        jPanel15.setLayout(new java.awt.GridLayout(1, 2));

        jLabel2.setText("Fecha de la Venta");
        jPanel15.add(jLabel2);

        jLabel3.setText("Fecha Actual");
        jPanel15.add(jLabel3);

        getContentPane().add(jPanel15);

        jPanel8.setLayout(new java.awt.GridLayout(1, 2));

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton1);

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton2);

        getContentPane().add(jPanel8);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

                Venta v = new Venta();
                Cliente c = new Cliente();
                Producto p = new Producto();
                v.setFecha(new Date());
                v.setId(Integer.parseInt(jTextField1.getText()));
                c.setId(Integer.parseInt(jTextField2.getText()));
                p.setCodigo(Integer.parseInt(jTextField3.getText()));
                c.setNombre(jComboBox4.getSelectedItem().toString());
                c.setSexo(jLabel5.getText());
                c.setTelefono(Integer.parseInt(jLabel6.getText()));
                c.setDireccion(jLabel7.getText());
                p.setNombre(jComboBox8.getSelectedItem().toString());
                p.setPrecio(Float.parseFloat(jLabel9.getText()));
                p.setDepartamento(jLabel10.getText());
                p.setDescripcion(jLabel1.getText());
                ventaControl.ventaregistrar(v);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jLabel5.setText("");
                jLabel6.setText("");
                jLabel7.setText("");
                jLabel9.setText("");
                jLabel10.setText("");
                jLabel11.setText("");
        
        //        Producto p = productoControl.buscarPorCodigo(Integer.parseInt(jTextField2.getText()));
        //        v.setproducto(p);
        //        ventaControl.registrar(v);
                
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            Producto p=productoControl.buscarPorCodigo(Integer.parseInt(jTextField3.getText()));
            if(p!=null){
                //jTextField1.setText(p.getNombre());
                jLabel9.setText(p.getPrecio()+"");
                jLabel10.setText(p.getDepartamento());
                jLabel11.setText(p.getDescripcion());
            }
        }
    }//GEN-LAST:event_jTextField3KeyPressed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2KeyPressed

        //Producto p = (prodcuto) 
                jComboBox4.getSelectedItem();
                System.out.println(jComboBox4);
        
//        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
//            Cliente c=clienteControl.clienbuscarPorCodigo(Integer.parseInt(jTextField2.getText()));
//            if(c!=null){
//                //jTextField1.setText(p.getNombre());
//                jLabel5.setText(c.getSexo());
//                jLabel6.setText(c.getTelefono()+"");
//                jLabel7.setText(c.getDireccion());
//            }
//                }
    }//GEN-LAST:event_jTextField2KeyPressed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox4ActionPerformed

    private void jComboBox4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBox4MouseClicked
        if(evt.getClickCount()==2){
            Cliente c=clienteControl.clienbuscarPorCodigo(Integer.parseInt(jComboBox4.getToolTipText()));
            if(c!=null){
                //jTextField1.setText(p.getNombre());
                jLabel5.setText(c.getSexo());
                jLabel6.setText(c.getTelefono()+"");
                jLabel7.setText(c.getDireccion());
            }
        }
    }//GEN-LAST:event_jComboBox4MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Etiqueta1;
    private javax.swing.JLabel Etiqueta10;
    private javax.swing.JLabel Etiqueta11;
    private javax.swing.JLabel Etiqueta2;
    private javax.swing.JLabel Etiqueta3;
    private javax.swing.JLabel Etiqueta4;
    private javax.swing.JLabel Etiqueta5;
    private javax.swing.JLabel Etiqueta6;
    private javax.swing.JLabel Etiqueta7;
    private javax.swing.JLabel Etiqueta8;
    private javax.swing.JLabel Etiqueta9;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
